﻿using System;
using System.Collections.Generic;
using System.Text;

namespace T.FAS.Runtime.Metadata.Dao
{
    /// <summary>
    /// 版本帮助类
    /// </summary>
    public class VersionHelper
    {
        public static bool IsGreater(string baseVersion, string userVersion)
        {
            if (string.IsNullOrEmpty(baseVersion))
                return true;
            if (string.IsNullOrEmpty(userVersion))
                return false;

            string[] arrBase = baseVersion.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            string[] arrUser = userVersion.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);

            if (arrBase.Length != 4 || arrUser.Length != 4)
                return true;

            for (int i = 0; i < 4; i++)
            {
                if (Convert.ToInt32(arrBase[i]) < Convert.ToInt32(arrUser[i]))
                    return true;
            }

            return false;
        }

        public static string GetNextVersion(string version)
        {
            string[] arrBase = version.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            if (arrBase.Length != 4)
                return GetInitVersion();
            arrBase[3] = Convert.ToString(Convert.ToUInt32(arrBase[3]) + 1);

            return string.Format("{0}.{1}.{2}.{3}", arrBase[0], arrBase[1], arrBase[2], arrBase[3]);
        }

        public static string GetPreVersion(string version)
        {
            string[] arrBase = version.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            if (arrBase.Length != 4)
                return GetInitVersion();
            arrBase[3] = Convert.ToString(Convert.ToUInt32(arrBase[3]) - 1);

            return string.Format("{0}.{1}.{2}.{3}", arrBase[0], arrBase[1], arrBase[2], arrBase[3]);
        }

        public static string GetInitVersion()
        {
            return "1.0.0.0";
        }

        public static decimal GetVerMask(string version)
        {
            if (string.IsNullOrEmpty(version))
                return 0;
            if (version.IndexOf(".") < 0)
                return (decimal)1 * 10000 * 10000 * 10000 + 1;
            string[] arrBase = version.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            if (arrBase.Length != 4)
                return (decimal)1 * 10000 * 10000 * 10000 + 1;
            else
                return Convert.ToDecimal(arrBase[0]) * 10000 * 10000 * 10000 +
                    Convert.ToDecimal(arrBase[1]) * 10000 * 10000 +
                    Convert.ToDecimal(arrBase[2]) * 10000 +
                    Convert.ToDecimal(arrBase[3]);
        }

        public static string GetVersion(decimal verMask)
        {
            string versionMask = Convert.ToString(verMask);
            if (versionMask.Length == 13)
            {
                string[] verArr = new string[4];
                verArr[0] = versionMask.Substring(0, 1);
                verArr[1] = Convert.ToInt32(versionMask.Substring(1, 4)) + "";
                verArr[2] = Convert.ToInt32(versionMask.Substring(5, 4)) + "";
                verArr[3] = Convert.ToInt32(versionMask.Substring(9, 4)) + "";
                versionMask = string.Format("{0}.{1}.{2}.{3}", verArr[0], verArr[1], verArr[2], verArr[3]);
            }
            return versionMask;
        }

        public static int GetIntegerVersion(string version)
        {
            if (string.IsNullOrEmpty(version))
                return 0;
            if (version.IndexOf(".") < 0)
                return 1;
            string[] arrBase = version.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            if (arrBase.Length != 4)
                return 1;
            else
                return Convert.ToInt32(arrBase[3]);
        }

        public static string GetLastedVersion(List<string> versions)
        {
            if (versions == null)
                return string.Empty;
            string lastVersion = string.Empty;

            foreach (var item in versions)
            {
                if (IsGreater(lastVersion, item) == true)
                    lastVersion = item;
            }

            return lastVersion;
        }
    }
}
